<?php
/**
 * 使用示例
 */
declare(strict_types=1);

namespace Apps\Http\Controllers\Demos;

use Libraries\Bases\ControllerBase;
use Libraries\Tpl;
use Libraries\Helper;
use Libraries\Dispatcher;
use Libraries\Connections\RedisConn;
use Apps\Http\Models\Demo as DemoModel;

class Index extends ControllerBase
{
    protected $blnStartSess = true;
    public $skipHooks = ['JwtAuth' => '*'];

    /**
     * 示例页入口
     * @return void
     */
    public function index(): void
    {
        $arrAssign = [
            'arrLinks' => [
                [
                    'text' => 'check code imgage (with session)',
                    'url' => '/demos/checkcode.png',
                ],
                [
                    'text' => 'phpinfo()',
                    'url' => '/demos/info.html',
                ],
                [
                    'text' => 'dump $_SESSION (session saved by cache file or redis)',
                    'url' => '/demos/sessions.html',
                ],
                [
                    'text' => 'show redis info (with redis connection pool)',
                    'url' => '/demos/redis_info.html',
                ],
                [
                    'text' => 'show all mysql users (with model base & pdo connection pool)',
                    'url' => '/demos/mysql_uesrs.html',
                ],
                [
                    'text' => 'show cached routes',
                    'url' => '/demos/cached_routes.html',
                ],
            ],
        ];
        $strHtml = Tpl::fetchHtml('demos/index.html', $arrAssign);
        $this->objResponse->end($strHtml);
    }
    
    /**
     * check code
     * @return void
     */
    public function checkcode(): void
    {
        $strCode = Helper::getRandStr(4);
        $this->arrSession['check_code'] = strtolower($strCode);
        $this->arrSession['check_code_time'] = time();
        $im = imagecreate(50, 20);
        imagecolorallocate($im, 255, 255, 255);
        $text_color = imagecolorallocate($im, 0, 0, 0);
        imagestring($im, 5, 5, 4 + mt_rand(-3, 3), $strCode[0], $text_color);
        imagestring($im, 5, 15, 4 + mt_rand(-3, 3), $strCode[1], $text_color);
        imagestring($im, 5, 25, 4 + mt_rand(-3, 3), $strCode[2], $text_color);
        imagestring($im, 5, 35, 4 + mt_rand(-3, 3), $strCode[3], $text_color);
        for ($xi = 20; $xi >= 0; $xi--) {
            imagesetpixel($im, mt_rand(0, 50), mt_rand(0, 20), $text_color);
        }
        ob_start();
        imagepng($im);
        $strImg = ob_get_clean();
        imagedestroy($im);
        $this->objResponse->header('content-type', 'image/png');
        $this->objResponse->end($strImg);
    }
    
    /**
     * phpinfo
     * @return void
     */
    public function info(): void
    {
        ob_start();
        phpinfo();
        $str = ob_get_clean();
        $this->objResponse->write('<xmp>' . var_export($str, true) . '</xmp>');
    }
    
    /**
     * session data
     * @return void
     */
    public function sessions(): void
    {
        $this->objResponse->end('<xmp>' . var_export($this->arrSession, true) . '</xmp>');
    }
    
    /**
     * redis info
     * @return void
     */
    public function redis_info(): void
    {
        $objRedis = RedisConn::get();
        $arrInfo = $objRedis->info();
        RedisConn::put($objRedis);
        $this->objResponse->end('<xmp>' . var_export($arrInfo, true) . '</xmp>');
    }
    
    /**
     * show all mysql users
     * @return void
     */
    public function mysql_uesrs(): void
    {
        $objModel = new DemoModel();
        $arrUser = $objModel->getUsers();
        $this->objResponse->end('<xmp>' . var_export($arrUser, true) . '</xmp>');
    }
    
    /**
     * show cached routes
     * @return void
     */
    public function cached_routes(): void
    {
        $this->objResponse->end('<xmp>' . var_export(Dispatcher::getCachedPaths(), true) . '</xmp>');
    }
}
